package util

import (
	"fmt"
	"github.com/smartwalle/alipay/v3"
	"strconv"
)

var (
	appId        string
	privateKey   string
	aliPublicKey string
	client       *alipay.Client
)

func init() {
	appId = "2088042505267968"
	privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUOMrgBF0e5YHlBDRkVjNVwBME45ti4kpw0UZJZDc73Mvc/vsxvqatcjgrjVdf+oHUvgcNwLNE2P6NcD5Ib4FqFpVBMspntdPq3+VSJde+Ijw1sFRYpzvWLpc1xGdC6YlWFAKhdjq1FFUr+sPpcS9Ci3GKTA/0ekEIo4pfGXQRReQq2T68crwemVlo/U4Jsg5iB1Wviz9w52WkgH3XomdTlA/9XVZFAIF+0/0S8c/fqv9gtMThmPmBghWRFvbQ/cCgmOkkoSag8U+NwgN3ubVkNfKX7uDLjs0yogOB1U45/fH1saei2WzfY/IzxhX0zPLoIsyM6JAUHZWYDMHHY1a9AgMBAAECggEAb6ICcgv3UoDKfwDWsMcXbLr32BPQgP4r0KJdTIM08loOFIjaCZIsmK9QzMRsDTZvuBnS2DHNEspXlZD257lLf+o4SV2Ggeu5CsnMPj2QKnxAWrNOI2L4twxqCC2/E+8t5220uYSBHMaOi0wXZ2c+j+DUB7PXxGqbNnVtjsht2V9ivQWHDN4YaxqdwfoLoh4UNfvau919s8u+A6uR5q+GIVWtp04YxUB0sOffT4mBiCQDSlifS1V3IV2Vyw9jw8EXYLL30yMQU9cf/OvaBdOF/whbwkSJ2keHt1hl1SdXDp/F6NB+5ny2BgZ1RqybTUaBomUc6l3tJihI9sWFj8II5QKBgQDUddCo9ASE2KwZYoSFOG7rG2Oqd4mwseydU3xgReiniu5AxN5gpi2xXhlqYCx/ddBgsirHRPU7jpLfCS5BPqFa3+QiIF8n6ZB40Hek3HEz3vlKf0XbR8TCeyUL2JQJV6VDQgjGw685c/BxvxNZlgBwcxM7c6LPM7dep5YIxdEbKwKBgQCymN/sMWjXWmh0tjY+aO6rPU2lq7HdPffySzWYyz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QIDAQAB"
	client, _ = alipay.New(appId, privateKey, false)
	client.LoadAliPayPublicKey(aliPublicKey)
}

func Pay(cn string, count int) string {
	var p = alipay.TradePagePay{}
	p.NotifyURL = ""
	p.ReturnURL = ""
	p.Subject = ""
	p.OutTradeNo = cn
	p.TotalAmount = strconv.Itoa(count)
	p.ProductCode = "FAST_INSTANT_TRADE_PAY"

	var url, err = client.TradePagePay(p)
	if err != nil {
		fmt.Println(err)
	}
	// 这个 payURL 即是用于打开支付宝支付页面的 URL，可将输出的内容复制，到浏览器中访问该 URL 即可打开支付页面。
	var payURL = url.String()
	fmt.Println(payURL)
	return payURL
}
